package com.example.administrator.compileapp.activity;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.administrator.compileapp.R;
import com.example.administrator.compileapp.databinding.ActivityStartBinding;

public class StartActivity extends AppCompatActivity {
    private ActivityStartBinding binding;
    private String loadUrl = "file:///android_asset/default.htm";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_start);
        binding.webviewStart.setInitialScale(20);//这里一定要设置，数值可以根据各人的需求而定，我这里设置的是50%的缩放

        WebSettings webSettings = binding.webviewStart.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(true);// support zoom
        webSettings.setUseWideViewPort(true);// 这个很关键
        webSettings.setLoadWithOverviewMode(true);
        init();
        binding.backStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if(getIntent().getStringExtra("title")==null){
             binding.titleStart.setText("欢迎进入编译原理移动学习平台");
        }else {
            binding.titleStart.setText(getIntent().getStringExtra("title"));
        }
    }

    private void init() {
        binding.webviewStart.loadUrl(loadUrl);
        binding.webviewStart.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
        {
            if(binding.webviewStart.canGoBack())
            {
                binding.webviewStart.goBack();//返回上一页面
                return true;
            }
            else
            {
                  System.exit(0);//退出程序
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
